﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DoubleLinkedList
{    
    public class DoubleLinkedList
    {
        public Node Head { get; private set; }
        public int Count { get; private set; }

        public DoubleLinkedList()
        {
            this.Count = 0;
            this.Head = null;
        }

        public void AddFirst(object data)
        {
            Node newNode = new Node(data);

            if (this.Head == null)
            {
                this.Head = newNode;
            }
            else
            {
                this.Head.Previous = newNode;
                newNode.Next = this.Head;
               
                this.Head = newNode;
            }

            this.Count++;
        }

        public void AddLast(object data)
        {
            Node newNode = new Node(data);

            if (this.Head == null)
            {
                this.Head = newNode;
            }
            else
            {
                Node current = this.Head;

                while (current.Next != null)
                    current = current.Next;

                current.Next = newNode;
                newNode.Previous = current;
            }

            this.Count++;
        }

        public void RemoveFirst()
        {
            if (this.Head != null)
            {
                this.Head = this.Head.Next;
                this.Head.Previous = null;

                this.Count--;
            }
        }

        public void RemoveLast()
        {
            if (this.Head != null)
            {
                Node current = this.Head;

                while (current.Next != null)
                {
                    current = current.Next;
                }

                current.Previous.Next = null;
                this.Count--;                
           }
        }

        public void Clear()
        {
            this.Head = null;
        }

        public void Display()
        {
            Node node = this.Head;

            while (node != null)
            {
                Console.Write(string.Format("<-| {0} |->", node.Data));
                node = node.Next;
            }

            Console.ReadLine();
            Console.Clear();
        } 
    }

    public class Node
    {
        private object _data;
        private Node _next;
        private Node _previous;

        public object Data
        {
            get { return _data; }
            set { _data = value; }
        }
        public Node Next
        {
            get { return _next; }
            set { _next = value; }
        }
        public Node Previous
        {
            get { return _previous; }
            set { _previous = value; }
        }

        public Node()
        {
            this.Data = null;
            this.Next = null;
            this.Previous = null;
        }

        public Node(object data)
            : this()
        {
            this._data = data;
        }
    }
}
